package org.winexpress
import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

class CountryController {

    // REST operations
    // GET
    def show = {
        println 'CountryController >> Show >> START'
        
        CountryManagement ctyman = new CountryManagement()
        
        if (params.id && Country.exists(params.id)) {    
            def cty = ctyman.getCountryByID(params.id)
            render cty as JSON
            
            println 'Show country by ID >> ' + cty.id + " " + cty
        }
        else
        if (params.countryName) {    
            def cty = ctyman.getCountryByCountryName(params.countryName)
            render cty as JSON
            
            println 'Show country by ID >> ' + cty.countryName + " " + cty.id
        }
        else {
            def allCTY = ctyman.listCountries()
            if (allCTY.length() > 0) {
                render allCTY as JSON
            }
            else {
                JSONObject o = new JSONObject()
                o.put("404",true)
                render o as JSON
            }  
            
            println 'Listing all countries >> ' + allCTY
        }
        
        println 'CountryController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'CountryController >> Update >> START'
        
        JSONObject o = request.JSON
        
        CountryManagement ctyman = new CountryManagement()
        
        def cty = ctyman.getCountryByID(o.get("id"))
        if (o.has("countryName")) { cty.countryName = o.get("countryName"); println cty.countryName }
        
        def result = ctyman.updateCountry(cty)
        if (result == true) {
            println 'CountryController >> Update >> ' + result
//            render response as JSON
        }
        else {
            println 'CountryController >> Update >> ' + result
        }
        

        println 'CountryController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'CountryController >> delete >> START'
        
        CountryManagement ctyman = new CountryManagement()
        
        if (params.id && Country.exists(params.id)) {    
            def result = ctyman.deleteCountry(id)
            
            if (result == true) {
            println 'CountryController >> delete >> ' + result
    //            render response as JSON
            }
            else {
                println 'CountryController >> delete >> ' + result
            }
        }
        
        println 'CountryController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'CountryController >> Save >> START'
        
        JSONObject o = request.JSON
        
        CountryManagement ctyman = new CountryManagement()
        
        def result = ctyman.addCountry(o)
        if (result == true) {
            println 'CountryController >> Save >> ' + result
//            render response as JSON
        }
        else {
            println 'CountryController >> Save >> ' + result
        }
        
        println 'CountryController >> Save >> END'
    }
}
